package com.zwh.mangomall.service.impl;

import com.zwh.mangomall.mapper.UserInfoMapper;
import com.zwh.mangomall.pojo.MangoUser;
import com.zwh.mangomall.service.UserInfoService;
import com.zwh.mangomall.service.ex.EmptyArgumentException;
import com.zwh.mangomall.service.ex.InsertException;
import com.zwh.mangomall.service.ex.LoginException;
import com.zwh.mangomall.util.IpUtil;
import com.zwh.mangomall.util.MatchUtil;
import com.zwh.mangomall.util.Md5Util;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class UserInfoServiceImpl implements UserInfoService{

    @Autowired
    UserInfoMapper userInfoMapper;

    @Value("${avatar}")
    private String avatar;

    @Override
    public boolean insertUserInfo(JSONObject jsonObject) {

        MangoUser mangoUser = new MangoUser();

        // 判断传入的参数是否含有必填项
        if (!jsonObject.containsKey("username") || !jsonObject.containsKey("password")
                || !jsonObject.containsKey("confirmPwd") || !jsonObject.containsKey("phone")){
            throw new EmptyArgumentException("注册信息不足");
        }

        // 判断用户名密码长度是否合法
        if (!MatchUtil.lengthUsername(jsonObject.getString("username"))){
            throw new InsertException("用户名长度不足");
        }
        if (!MatchUtil.lengthPassword(jsonObject.getString("password"))){
            throw new InsertException("密码长度不足");
        }

        // 判断密码和确认密码是否一致
        if (!jsonObject.getString("password").equals(jsonObject.getString("confirmPwd"))){
            throw new InsertException("密码不一致");
        }

        // 判断手机格式是否合法
        if(!MatchUtil.isPhone(jsonObject.getString("phone"))){
            throw new InsertException("手机号不合法");
        }

        // 判断手机号是否已存在
        if (userInfoMapper.selectPhone(jsonObject.getString("phone")) != null){
            throw new InsertException("手机号已存在");
        }

        mangoUser.setUsername(jsonObject.getString("username"));
        mangoUser.setPassword(jsonObject.getString("password"));
        mangoUser.setPhone(jsonObject.getString("phone"));

        if (jsonObject.containsKey("gender")){
            mangoUser.setGender(jsonObject.getInt("gender"));
        }
        if (jsonObject.containsKey("email")){
            mangoUser.setEmail(jsonObject.getString("email"));
        }
        if (jsonObject.containsKey("containsKey") && !jsonObject.getString("birthday").equals("")){
            try {
                mangoUser.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse(jsonObject.getString("birthday")));
            } catch (ParseException e) {
                throw new InsertException("生日日期格式非法");
            }
        }

        mangoUser.setCreatedUser(jsonObject.getString("ip"));

        // 加密处理
        String salt = UUID.randomUUID().toString().replace("-", "");
        mangoUser.setSalt(salt);
        mangoUser.setPassword(Md5Util.passwordMD5(salt, mangoUser.getPassword()));

        // 设置用户状态，正常存在1
        mangoUser.setUserStatus(1);

        // 添加创建日期等信息
        mangoUser.setCreatedTime(new Date());
        mangoUser.setModifiedUser(mangoUser.getCreatedUser());
        mangoUser.setModifiedTime(new Date());

        // 存入数据库
        if(userInfoMapper.insertMangoUser(mangoUser) != 1){
            throw new InsertException("注册失败");
        }

        return true;

    }

    @Override
    public MangoUser userLogin(JSONObject jsonObject) {

        // 判断传入的参数是否含有必填项
        String phone = jsonObject.getString("phone");
        String password = jsonObject.getString("password");
        if(!jsonObject.containsKey("phone") || !jsonObject.containsKey("password")){
            throw new EmptyArgumentException("登录信息不足");
        }

        // 判断手机号是否存在
        if (userInfoMapper.selectPhone(phone) == null){
            throw new InsertException("手机号不存在");
        }

        // 判断该账号是否正常
        if (userInfoMapper.selectStatus(phone) != 1){
            throw new LoginException("账号状态异常，请联系客服");
        }

        // 检验密码是否匹配
        MangoUser mangoUser = userInfoMapper.selectMangoUserDetail(phone);
        String md5 = Md5Util.passwordMD5(mangoUser.getSalt(), password);
        if (!md5.equals(mangoUser.getPassword())){
            throw new LoginException("密码错误");
        }

        // 信息脱敏
        mangoUser.setSalt(null);

        return mangoUser;
    }

    @Override
    public List<MangoUser> listUserInfos() {
        return null;
    }

    @Override
    public int deleteUserInfo(String mangoIdOrPhone) {
        return 0;
    }

    @Override
    public int deleteListUserInfos() {
        return 0;
    }

    @Override
    public int updateUserInfo(String mangoIdOrPhone) {
        return 0;
    }


}
